* =====
* main regression models
* =====

use "./data/processed/CE_cleaned_reg.dta",clear 

estimates clear 
eststo clear 

* ==============================
* Table 1. models for network isolation and exposure to dissent
* ==============================
local demo_controls r_age r_female c.r_educ i.r_race r_married r_working
local DV isolation
qui logit `DV' i.year i.state_code `demo_controls' close_election [pw=wt] if  a_talkpol > 0 & one == 1, cluster(state_code)
	estadd local state_year_fe "Yes"
estimates store est1
margins, dydx(close_election) post 
	estadd local state_year_fe "Yes"
	estadd local demo_control "Yes" 	 
	estadd local pol_control "No" 	 
estimates store m1

qui logit `DV' i.year i.state_code `demo_controls' close_election r_pol_interest r_party_intensity [pw=wt] if a_talkpol > 0 & one == 1, cluster(state_code)
	estadd local state_year_fe "Yes"
estimates store est2
margins, dydx(close_election) post 
	estadd local state_year_fe "Yes"
	estadd local demo_control "Yes" 	 
	estadd local pol_control "Yes" 	 
estimates store m2

local DV pol_one_diff
qui logit `DV' i.year i.state_code `demo_controls' close_election [pw=wt] if a_talkpol > 0 &  one == 1, cluster(state_code)
	estadd local state_year_fe "Yes"
estimates store est3
margins, dydx(close_election) post 
	estadd local state_year_fe "Yes"
	estadd local demo_control "Yes" 	 
	estadd local pol_control "No" 	 
estimates store m3

qui logit `DV' i.year i.state_code `demo_controls' close_election r_pol_interest r_party_intensity [pw=wt] if a_talkpol > 0 &  one == 1, cluster(state_code)
	estadd local state_year_fe "Yes"
estimates store est4
margins, dydx(close_election) post 
	estadd local state_year_fe "Yes"
	estadd local demo_control "Yes" 	 
	estadd local pol_control "Yes" 	 
estimates store m4

* export table
esttab m1 m2 m3 m4 using "./output/table_main_isolation_homophily_margin.rtf", replace rtf ///	
	b(%9.3f) se(%9.3f) star(+ 0.10 * 0.05 ** 0.01)   ///
		mgroups("Network Isolation" "Cross-cutting Exposure", pattern(1 0 1 0))  ///
		title("The impact of close elections on network isolation and exposure to dissent.")   ///
		label legend stats (N state_year_fe demo_control pol_control, fmt(%9.0gc %9.0gc %9.0gc %9.0gc) ///
		label("N" "State-year FE" "baseline controls" "political controls")) ///
		varlabels(_cons "Constant")  nonotes 	 ///
		mtitles("model1" "model2" "model3" "model4") 	

esttab est1 est2 est3 est4 using "./output/table_main_isolation_homophily_reg.rtf", replace rtf ///	
	drop(*.state_code *.year) ///
	b(%9.3f) se(%9.3f) star(+ 0.10 * 0.05 ** 0.01)   ///
		mgroups("Network Isolation" "Cross-cutting Exposure", pattern(1 0 1 0))  ///
		title("The impact of close elections on network isolation and exposure to dissent.")   ///
		label legend stats (N state_year_fe demo_control pol_control, fmt(%9.0gc %9.0gc %9.0gc %9.0gc) ///
		label("N" "State-year FE" "baseline controls" "political controls")) ///
		varlabels(_cons "Constant")  nonotes 	 ///
		mtitles("model1" "model2" "model3" "model4") 	

* ==============================
* Table 2. models for talking to the middle and talking to relatives
* ==============================
 local demo_controls r_age r_female c.r_educ i.r_race r_married r_working
local DV i_middle
logit `DV' i.year i.state_code  `demo_controls'  close_election [pw=wt], cluster(state_code), if a_talkpol > 0 & one == 1
	estadd local state_year_fe "Yes"
estimates store est3
margins, dydx(close_election) post 
	estadd local state_year_fe "Yes"
	estadd local demo_control "Yes" 	 
	estadd local pol_control "No" 	 
estimates store m3

logit `DV' i.year i.state_code  `demo_controls'  close_election r_pol_interest r_party_intensity [pw=wt], cluster(state_code), if a_talkpol > 0 & one == 1
	estadd local state_year_fe "Yes"
estimates store est4
margins, dydx(close_election) post 
	estadd local state_year_fe "Yes"
	estadd local demo_control "Yes" 	 
	estadd local pol_control "Yes" 	 
estimates store m4

local DV i_relative
logit `DV' i.year i.state_code  `demo_controls'  close_election [pw=wt], cluster(state_code), if a_talkpol > 0 & one == 1
	estadd local state_year_fe "Yes"
estimates store est5
margins, dydx(close_election) post 
	estadd local state_year_fe "Yes"
	estadd local demo_control "Yes" 	 
	estadd local pol_control "No" 	 
estimates store m5

logit `DV' i.year i.state_code  `demo_controls'  close_election r_pol_interest r_party_intensity [pw=wt], cluster(state_code), if a_talkpol > 0 & one == 1
	estadd local state_year_fe "Yes"
estimates store est6
margins, dydx(close_election) post 
	estadd local state_year_fe "Yes"
	estadd local demo_control "Yes" 	 
	estadd local pol_control "Yes" 	 
estimates store m6

esttab m3 m4 m5 m6 using "./output/table_main_middle_kinship_margin.rtf", replace rtf ///	
		b(%9.3f) se(%9.3f) star(+ 0.10 * 0.05 ** 0.01) ///
		mgroups("Talking to the middle" "Talking to relatives", pattern(1 0 1 0))  ///
		label legend stats (N state_year_fe demo_control pol_control, fmt(%9.0gc %9.0gc %9.0gc %9.0gc) ///
		label("N" "State-year FE" "baseline controls" "political controls"))  ///
		varlabels(_cons "Constant")  nonotes 	 ///
		mtitles("model1" "model2" "model3" "model4" ) 	///
		title("The impact of close elections on talking to nonpartisans/nonvoters, and relatives.") 

esttab est3 est4 est5 est6 using "./output/table_main_middle_kinship_reg.rtf", replace rtf  ///
	drop(*.state_code *.year) ///
	b(%9.3f) se(%9.3f) star(+ 0.10 * 0.05 ** 0.01) ///
		mgroups("Talking to the middle" "Talking to relatives", pattern(1 0 1 0))  ///
		label legend stats (N state_year_fe, fmt(%9.0gc %9.0gc)  ///
		label("N" "State-year FE")) ///
		varlabels(_cons "Constant")  nonotes 	 ///
		mtitles("model1" "model2" "model3" "model4" ) 	///
		title("The impact of close elections on talking to nonpartisans/nonvoters, and relatives.") 

* ==============================
* Table 3. effects of political advertisement on network isolation and cross-cutting exposure
* ==============================
gen X_n_ad = .
gen X_p_attack = .

local demo_controls r_age r_female c.r_educ i.r_race r_married r_working
local ad_var X_n_ad X_p_attack  

local DV isolation
replace X_n_ad = std_n_ad 
replace X_p_attack = p_attack
label var X_n_ad "Frequency of Political Ads"
label var X_p_attack "P(Ad tone = Attack)"

qui logit `DV' i.year i.state_code `demo_controls'  `ad_var'  r_pol_interest r_party_intensity  [pw=wt] if a_talkpol > 0 & one == 1, cluster(state_code)
	estadd local state_year_fe "Yes"
estimates store est1
margins, dydx(`ad_var') post 
	estadd local state_year_fe "Yes"
	estadd local demo_control "Yes" 	 
	estadd local pol_control "Yes" 	 
	estadd local aggregate "Yes" 	 
estimates store m1

replace X_n_ad = std_n_ad_year 
replace X_p_attack = p_attack_year
label var X_n_ad "Frequency of Political Ads"
label var X_p_attack "P(Ad tone = Attack)"

qui logit `DV' i.year i.state_code `demo_controls'  `ad_var'  r_pol_interest r_party_intensity  [pw=wt] if a_talkpol > 0 & one == 1, cluster(state_code)
	estadd local state_year_fe "Yes"
estimates store est2
margins, dydx(`ad_var') post 
	estadd local state_year_fe "Yes"
	estadd local demo_control "Yes" 	 
	estadd local pol_control "Yes" 	 
	estadd local aggregate "No" 	 
estimates store m2


local DV pol_one_diff

replace X_n_ad = std_n_ad 
replace X_p_attack = p_attack
label var X_n_ad "Frequency of Political Ads"
label var X_p_attack "P(Ad tone = Attack)"

qui logit `DV' i.year i.state_code `demo_controls'  `ad_var'  r_pol_interest r_party_intensity  [pw=wt] if a_talkpol > 0 &  one == 1, cluster(state_code)
	estadd local state_year_fe "Yes"
estimates store est3
margins, dydx(`ad_var') post 
	estadd local state_year_fe "Yes"
	estadd local demo_control "Yes" 	 
	estadd local pol_control "Yes" 	 
	estadd local aggregate "Yes" 	 
estimates store m3

replace X_n_ad = std_n_ad_year 
replace X_p_attack = p_attack_year
label var X_n_ad "Frequency of Political Ads"
label var X_p_attack "P(Ad tone = Attack)"

qui logit `DV' i.year i.state_code `demo_controls'  `ad_var'  r_pol_interest r_party_intensity  [pw=wt] if a_talkpol > 0 & one == 1, cluster(state_code)
	estadd local state_year_fe "Yes"
estimates store est4
margins, dydx(`ad_var') post 
	estadd local state_year_fe "Yes"
	estadd local demo_control "Yes" 	 
	estadd local pol_control "Yes" 	 
	estadd local aggregate "No" 	 
estimates store m4

esttab m1 m2 m3 m4 using "./output/table_main_campaign.rtf", replace rtf  ///	
	b(%9.3f) se(%9.3f) star(+ 0.10 * 0.05 ** 0.01)  ///
		mgroups("Network Isolation" "Cross-cutting Exposure" , pattern(1 0 1 0))  ///
		title("The impact of political advertisement on network isolation and cross-cutting exposure")   ///
		label legend stats (N state_year_fe demo_control pol_control aggregate, fmt(%9.0gc %9.0gc %9.0gc %9.0gc %9.0gc)  ///
		label("N" "State-year FE" "baseline controls" "political controls" "daily matching")) ///
		varlabels(_cons "Constant")  nonotes 	 ///
		mtitles("model1" "model2" "model3" "model4") 	

esttab est1 est2 est3 est4 using "./output/table_main_campaign_reg.rtf", replace rtf ///
	drop(*.state_code *.year) ///
	b(%9.3f) se(%9.3f) star(+ 0.10 * 0.05 ** 0.01)  ///
		mgroups("Network Isolation" "Cross-cutting Exposure" , pattern(1 0 1 0))  ///
		title("The impact of political advertisement on network isolation and cross-cutting exposure")   ///
		label legend stats (N state_year_fe, fmt(%9.0gc %9.0gc) ///
		label("N" "State-year FE")) ///
		varlabels(_cons "Constant")  nonotes 	 ///
		mtitles("model1" "model2" "model3" "model4") 	

 

